package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsArticle;
import com.uyayo.cms.entity.CmsArticleLike;
import com.uyayo.cms.entity.CmsComment;
import com.uyayo.cms.entity.CmsCommentLike;
import com.uyayo.cms.mapper.CmsCommentLikeMapper;
import com.uyayo.cms.mapper.CmsCommentMapper;
import com.uyayo.cms.model.CmsArticleCountBO;
import com.uyayo.cms.model.CmsCommentBO;
import com.uyayo.cms.model.CmsCommentCondition;
import com.uyayo.cms.model.CmsCommentCountBO;
import com.uyayo.cms.service.CmsArticleService;
import com.uyayo.cms.service.CmsCommentService;
import com.uyayo.framework.common.exception.CodeMultipleException;
import com.uyayo.framework.common.exception.ParentNotFoundException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsCommentService")
public class CmsCommentServiceImpl implements CmsCommentService {

	@Autowired
	private CmsCommentMapper cmsCommentMapper;

	@Autowired
	private CmsCommentLikeMapper cmsCommentLikeMapper;

	@Autowired
	private CmsArticleService cmsArticleService;

	@Override
	public CmsComment getCommentById(Long id){
		return cmsCommentMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public List<CmsComment> selectComment(CmsCommentCondition cmsCommentCondition) {
		Example example = new Example(CmsComment.class);
		Criteria criteria = example.createCriteria();
		if(Objects.nonNull(cmsCommentCondition.getStatus())){
			criteria.andEqualTo("status",cmsCommentCondition.getStatus());
		}
		if(Objects.nonNull(cmsCommentCondition.getAuditStatus())){
			criteria.andEqualTo("auditStatus",cmsCommentCondition.getAuditStatus());
		}
		if(Objects.nonNull(cmsCommentCondition.getArticleId())){
			criteria.andEqualTo("articleId",cmsCommentCondition.getArticleId());
		}
		return cmsCommentMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsComment> selectCommentByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsCommentCondition cmsCommentCondition) {

		List<CmsComment> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsComment.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(Objects.nonNull(cmsCommentCondition.getArticleId())){
			criteria.andEqualTo("articleId",cmsCommentCondition.getArticleId());
		}
		if(!StringUtils.isEmpty(cmsCommentCondition.getContent())){
			criteria.andLike("content","%"+cmsCommentCondition.getContent()+"%");
		}
		if(cmsCommentCondition.getBeginCreateTime()!=null || cmsCommentCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsCommentCondition.getBeginCreateTime(), cmsCommentCondition.getEndCreateTime());
		}
		userList = cmsCommentMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsComment> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}

	@Override
	public PageBean<CmsCommentBO> selectCmsCommentListByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsCommentCondition cmsCommentCondition) {

		List<CmsCommentBO> userList;
		//分页处理
		PageHelper.startPage(pageCurrent, pageSize);
		userList = cmsCommentMapper.selectCmsCommentList(cmsCommentCondition);
		//取记录总条数
		PageInfo<CmsCommentBO> pageInfo = new PageInfo<>(userList);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}


	@Override
	public int createCmsComment(CmsComment cmsComment){
		CmsArticleCountBO cmsArticleCountBO = new CmsArticleCountBO();
		cmsArticleCountBO.setId(cmsComment.getArticleId());
		cmsArticleCountBO.setCommentCount(1);
		cmsArticleService.updateArticleCount(cmsArticleCountBO);
		return cmsCommentMapper.insertSelective(cmsComment);
	}

	private int selectCountByMemberId(Long memberId) {
		CmsComment cmsComment = new CmsComment();
		cmsComment.setMemberId(memberId);
		return cmsCommentMapper.selectCount(cmsComment);
	}

	@Override
	public CmsComment selectOne(CmsComment cmsComment) {
		return cmsCommentMapper.selectOne(cmsComment);
	}

	@Override
	public int updateComment(CmsComment cmsComment){
		return cmsCommentMapper.updateByPrimaryKeySelective(cmsComment);
	}

	@Override
	public int deleteComment(Long id) {
		return cmsCommentMapper.deleteByPrimaryKey(id);
	}


	@Override
	public int updateCommentCount(CmsCommentCountBO cmsCommentCountBO) {
		CmsComment cmsComment = new CmsComment();
		cmsComment.setId(cmsCommentCountBO.getId());
		cmsComment = this.selectOne(cmsComment);
		if(Objects.nonNull(cmsCommentCountBO.getReplyCount())){
			cmsComment.setReplyCount(cmsComment.getReplyCount()+cmsCommentCountBO.getReplyCount());
		}

		if(Objects.nonNull(cmsCommentCountBO.getLikeCount())){
			cmsComment.setLikeCount(cmsComment.getLikeCount()+cmsCommentCountBO.getLikeCount());
		}
		if(Objects.nonNull(cmsCommentCountBO.getDislikeCount())){
			cmsComment.setDislikeCount(cmsComment.getDislikeCount()+cmsCommentCountBO.getDislikeCount());
		}

		return cmsCommentMapper.updateByPrimaryKeySelective(cmsComment);
	}




	@Override
	public int like(CmsCommentLike cmsCommentLike) {
		CmsCommentLike cmsLikeCondition = new CmsCommentLike();
		cmsLikeCondition.setCommentId(cmsCommentLike.getCommentId());
		cmsLikeCondition.setLikeType(cmsCommentLike.getLikeType());
		cmsLikeCondition.setMemberId(cmsCommentLike.getMemberId());
		List<CmsCommentLike> cmsCommentLikeList = cmsCommentLikeMapper.select(cmsLikeCondition);
		if(cmsCommentLikeList != null && cmsCommentLikeList.size()==1){//查询出数据才进行下一步
			CmsCommentLike cmsCommentLikeDB = cmsCommentLikeList.get(0);
			if(cmsCommentLikeList.get(0).getStatus().equals(cmsCommentLike.getStatus())){ //状态和数据库里的相同则无需处理
				return 0;
			}

			CmsCommentCountBO cmsCommentCountBO = new CmsCommentCountBO();
			cmsCommentCountBO.setId(cmsCommentLike.getCommentId());
			if(cmsCommentLike.getLikeType()==1 && cmsCommentLike.getStatus()==0){ //取消赞
				cmsCommentLikeDB.setStatus((short) 0);
				cmsCommentCountBO.setLikeCount(-1);
				this.updateCommentCount(cmsCommentCountBO);
			}else if(cmsCommentLike.getLikeType()==1 && cmsCommentLike.getStatus()==1){	//赞
				cmsCommentLikeDB.setStatus((short) 1);
				cancelDislike(cmsCommentLike.getCommentId(),cmsCommentLike.getMemberId());
			}else if(cmsCommentLike.getLikeType()==2 && cmsCommentLike.getStatus()==0){	//取消踩
				cmsCommentLikeDB.setStatus((short) 0);
				cmsCommentCountBO.setDislikeCount(-1);
				this.updateCommentCount(cmsCommentCountBO);
			}else {	//踩(cmsCommentLike.getLikeType()==2 && cmsCommentLike.getStatus()==1)
				cmsCommentLikeDB.setStatus((short) 1);
				cancelLike(cmsCommentLike.getCommentId(),cmsCommentLike.getMemberId());
			}
			return  this.cmsCommentLikeMapper.updateByPrimaryKeySelective(cmsCommentLikeDB);
		}else{
			if(cmsCommentLike.getLikeType()==2){
				cancelLike(cmsCommentLike.getCommentId(),cmsCommentLike.getMemberId());
			}else {
				cancelDislike(cmsCommentLike.getCommentId(),cmsCommentLike.getMemberId());
			}
			return cmsCommentLikeMapper.insertSelective(cmsCommentLike);
		}
	}

	/**
	 * 取消赞
	 * @param commentId 评论ID
	 * @param memberId 会员ID
	 */
	private void cancelLike(Long commentId,Long memberId){
		CmsCommentLike cmsDisLikeOppositeCondition = new CmsCommentLike();
		cmsDisLikeOppositeCondition.setCommentId(commentId);
		cmsDisLikeOppositeCondition.setLikeType((short) 1);
		cmsDisLikeOppositeCondition.setMemberId(memberId);
		CmsCommentLike cmsCommentDisLikeOppositeDB = cmsCommentLikeMapper.selectOne(cmsDisLikeOppositeCondition);
		if(cmsCommentDisLikeOppositeDB!=null  && cmsCommentDisLikeOppositeDB.getStatus()==1){
			cmsCommentDisLikeOppositeDB.setStatus((short) 0);
			this.cmsCommentLikeMapper.updateByPrimaryKeySelective(cmsCommentDisLikeOppositeDB);

			//评论的赞数量-1
			CmsCommentCountBO cmsCommentDisLikeCountOppositeBO = new CmsCommentCountBO();
			cmsCommentDisLikeCountOppositeBO.setId(commentId);
			cmsCommentDisLikeCountOppositeBO.setLikeCount(-1);
			this.updateCommentCount(cmsCommentDisLikeCountOppositeBO);
		}

		//评论的踩数量+1
		CmsCommentCountBO cmsCommentCountBO = new CmsCommentCountBO();
		cmsCommentCountBO.setId(commentId);
		cmsCommentCountBO.setDislikeCount(1);
		this.updateCommentCount(cmsCommentCountBO);
	}

	/**
	 * 取消踩
	 * @param commentId 评论ID
	 * @param memberId 会员ID
	 */
	private void cancelDislike(Long commentId,Long memberId){
		//取消踩
		CmsCommentLike cmsLikeOppositeCondition = new CmsCommentLike();
		cmsLikeOppositeCondition.setCommentId(commentId);
		cmsLikeOppositeCondition.setLikeType((short) 2);
		cmsLikeOppositeCondition.setMemberId(memberId);
		CmsCommentLike cmsCommentLikeOppositeDB = cmsCommentLikeMapper.selectOne(cmsLikeOppositeCondition);
		if(cmsCommentLikeOppositeDB!=null  && cmsCommentLikeOppositeDB.getStatus()==1){
			cmsCommentLikeOppositeDB.setStatus((short) 0);
			this.cmsCommentLikeMapper.updateByPrimaryKeySelective(cmsCommentLikeOppositeDB);

			//评论的踩数量-1
			CmsCommentCountBO cmsCommentLikeCountOppositeBO = new CmsCommentCountBO();
			cmsCommentLikeCountOppositeBO.setId(commentId);
			cmsCommentLikeCountOppositeBO.setDislikeCount(-1);
			this.updateCommentCount(cmsCommentLikeCountOppositeBO);
		}

		//评论的赞数量+1
		CmsCommentCountBO cmsCommentCountBO = new CmsCommentCountBO();
		cmsCommentCountBO.setId(commentId);
		cmsCommentCountBO.setLikeCount(1);
		this.updateCommentCount(cmsCommentCountBO);
	}

	
}
